#! /usr/bin/env python
# -*- coding: utf-8 -*-

import pytest
from playwright.sync_api import sync_playwright

from conf.base_conf import BaseConf


@pytest.fixture(scope='session')
def login():
    # 开始测试之前先进行登录，此处使用session级别，整个测试只用登录一次
    with sync_playwright() as playwright:
        browser_type = getattr(playwright, BaseConf.BROWSER)
        browser = browser_type.launch(headless=BaseConf.HEADLESS)
        context = browser.new_context()
        page = context.new_page()
        # 全局设置自动等待时间
        page.set_default_timeout(BaseConf.TIMEOUT)
        # 设置页面窗口大小
        page.set_viewport_size({"width": 1920, "height": 1080})
        page.goto(BaseConf.BASE_URL)
        page.get_by_placeholder('账号').fill(BaseConf.ACCOUNT)
        page.get_by_placeholder('密码').fill(BaseConf.PASSWORD)
        # 验证码输入1111即可放行，这里是让前端做了特殊处理
        page.get_by_placeholder('验证码').fill('1111')
        page.get_by_role('button', name='登录').click()
        yield page
        context.close()
        browser.close()


@pytest.fixture(scope='session')
def login_park():
    # 开始测试之前先进行登录，此处使用session级别，整个测试只用登录一次
    with sync_playwright() as playwright:
        browser_type = getattr(playwright, BaseConf.BROWSER)
        browser = browser_type.launch(headless=BaseConf.HEADLESS)
        context = browser.new_context()
        page = context.new_page()
        # 全局设置自动等待时间
        page.set_default_timeout(BaseConf.TIMEOUT)
        # 设置页面窗口大小
        page.set_viewport_size({"width": 1836, "height": 872})
        page.goto(BaseConf.BASE_URL)
        page.get_by_placeholder('账号').fill(BaseConf.ACCOUNT_PARK)
        page.get_by_placeholder('密码').fill(BaseConf.PASSWORD_PARK)
        # 验证码输入1111即可放行，这里是让前端做了特殊处理
        page.get_by_placeholder('验证码').fill('1111')
        page.get_by_role('button', name='登录').click()
        yield page
        context.close()
        browser.close()
